package com.css.test1.second;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.css.test1.R;

import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by css on 2017/11/7.
 */

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.cb_rember)
    CheckBox cbRember;
    @BindView(R.id.tv_leigin)
    TextView tvLeigin;
    @BindView(R.id.bt_cancel)
    Button btCancel;
    @BindView(R.id.bt_sure)
    Button btSure;
    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    private View regrest;
    private EditText reName;
    private EditText rePwd;
    private EditText reEmail;
    private Button bt_cancel;
    private Button bt_sure;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_second);
        ButterKnife.bind(this);
        builder = new AlertDialog.Builder(this);
        alertDialog = builder.create();
        LayoutInflater inflater = getLayoutInflater();
        regrest = inflater.inflate(R.layout.dialog_regrest, null);
        alertDialog.setView(regrest);
        reName = (EditText) regrest.findViewById(R.id.ed_d_name);
        rePwd = (EditText) regrest.findViewById(R.id.ed_d_pwd);
        reEmail = (EditText) regrest.findViewById(R.id.ed_d_email);
        bt_cancel = (Button) regrest.findViewById(R.id.bt_cancel);
        bt_sure = (Button) regrest.findViewById(R.id.bt_sure);

        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                reName.setText("");
                rePwd.setText("");
                reEmail.setText("");
            }
        });
        bt_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String name = reName.getText().toString();
                String pwd = rePwd.getText().toString();
                String email = reEmail.getText().toString();
                String namer = "[a-zA-Z]{6,12}";
                if (!Pattern.matches(namer, name)) {
                    Toast.makeText(getApplicationContext(), "用户名格式错误", Toast.LENGTH_SHORT).show();
                } else if (!Pattern.matches("[a-zA-Z0-9]{3,6}", pwd)) {
                    Toast.makeText(getApplicationContext(), "密码格式错误", Toast.LENGTH_SHORT).show();
                } else if (!Pattern.matches("^[A-Za-z\\d]+([-_.][A-Za-z\\d]+)*@([A-Za-z\\d]+[-.])+[A-Za-z\\d]{2,4}$", email)) {
                    Toast.makeText(getApplicationContext(), "邮箱格式错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    @OnClick({R.id.tv_leigin, R.id.bt_cancel, R.id.bt_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_leigin:
                regrest();
                break;
            case R.id.bt_cancel:
                finish();
                break;
            case R.id.bt_sure:
                login();
                break;
        }
    }

    private void regrest() {
        alertDialog.show();
    }


    private void login() {
        String name = etName.getText().toString();
        String pwd = etPwd.getText().toString();
        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pwd)) {
            if (name.equals("123") && pwd.equals("123")) {
                Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(getApplicationContext(), "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
        }
    }
}
